#include "StoryLayer.h"
#include "Constants.h"
#include "resource.h"
#include "PlayScene.h"
#include "MainScene.h"
#include "SimpleAudioEngine.h"
using namespace CocosDenshion;
StoryLayer::StoryLayer(){
}



StoryLayer::~StoryLayer(){

}

Scene * StoryLayer::createScene(){
	auto scene = Scene::create();
	StoryLayer *layer = StoryLayer::create();
	scene->addChild(layer);
	return scene;

}
bool StoryLayer::init(){
	if (!Layer::init()){
		return false;
	}
		auto director = Director::getInstance();
		auto glview = director->getOpenGLView();
		Size frameSize = glview->getFrameSize();
		int w = frameSize.width;
		int h = frameSize.height;

		if (h == 480 && w == 800)
		{
			//good
		}
		else if (h == 600 && w == 800)
		{
			//small   0.8 big 1
			Director::getInstance()->setContentScaleFactor(0.8);
		}
		else if (h == 540 && w == 960)
		{
			//small 0.83333   big    0.88889
			Director::getInstance()->setContentScaleFactor(0.83333);
		}
		else if (h == 768 && w == 1136)
		{
			//small  0.625    big 0.7042
			Director::getInstance()->setContentScaleFactor(0.625);
		}
		else if (h == 720 && w == 1280)
		{
			// small  0.625   big 0.66667
			Director::getInstance()->setContentScaleFactor(0.625);
		}
		else if (h == 480 && w == 854)
		{
			//small  0.9367  big 1
			Director::getInstance()->setContentScaleFactor(0.9367);
		}
		else if (h == 1080 && w == 1920)
		{
			//small 0.4167  big   0.4444444
			Director::getInstance()->setContentScaleFactor(0.4167);
		}
		else if (h == 1080 && w == 1794)
		{
			//small 0.4167  big   0.4444444
			Director::getInstance()->setContentScaleFactor(0.4167);
		}
		else if (h == 320 && w == 480)
		{
			//small  1.5   big  1.666667
			Director::getInstance()->setContentScaleFactor(1.5);
		}else if(h == 1080 && w == 1800 ){
			//small  0.4444   big  0.444
			Director::getInstance()->setContentScaleFactor(0.444);
		}else if (h == 800 && w == 1280){
			// small 0.6   big  0.625
			Director::getInstance()->setContentScaleFactor(0.6);
		}else if(h == 1152 && w == 1920 ){
			//small  0.41666   big  0.41666
			Director::getInstance()->setContentScaleFactor(0.41666);
		}else if(h == 1080 && w == 1920 ){
			//small  0.4444   big  0.41666
			Director::getInstance()->setContentScaleFactor(0.4166);
		}else if(h == 1536 && w == 2560 ){
			//small 0.3125    big  0.3125
			Director::getInstance()->setContentScaleFactor(0.3125);
		}else{
			Director::getInstance()->setContentScaleFactor(0.5);
		}


	auto size = Director::getInstance()->getVisibleSize();
	auto zeroPos = Director::getInstance()->getVisibleOrigin();
	auto centerPos = Point(size.width / 2, size.height / 2);


	//创建故事页面
	auto *spriteBG = Sprite::create(STORY_BG);
	spriteBG->setPosition(centerPos);
	this->addChild(spriteBG);

	//创建主菜单
	auto *menu = Menu::create();
	menu->setPosition(zeroPos);
	//添加菜单选项
	auto * menuCheckPoint = MenuItemSprite::create(Sprite::create(BEGIN_BUTTON), Sprite::create(BEGIN_BUTTON_PRESS)
		, CC_CALLBACK_1(StoryLayer::start, this));
	menuCheckPoint->setPosition(Point(centerPos.x, menuCheckPoint->getContentSize().height));
	menu->addChild(menuCheckPoint);

	this->addChild(menu);
	return true;
}
void StoryLayer::start(cocos2d::Ref* pSender){
	//确定音效开关，虽然没有启动到有音效开关的主场景，但是这个设置可以被保存到下次程序启动的时候
	if (CCUserDefault::getInstance()->getBoolForKey("sound", true)){
		CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect(MUSIC_EFFECT, false);//false表示不循环
	}
	//切换到主场景
	auto mainScene = TransitionFade::create(1, MainScene::createScene());
	Director::getInstance()->replaceScene(mainScene);
}
//预加载各种音效文件
void StoryLayer::preLoadMusic(){
	SimpleAudioEngine * audioEngin = SimpleAudioEngine::getInstance();
	audioEngin->preloadBackgroundMusic(MUSIC_EFFECT);
	audioEngin->preloadBackgroundMusic(MUSIC_WIN);
	audioEngin->preloadBackgroundMusic(MUSIC_LOSE);
}

